<?php


namespace Mslynn\Live\Block;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\Unserialize\Unserialize;
use Magento\Setup\Module\Di\Compiler\Log\Log;
use Monolog\Logger;
use Mslynn\Live\Helper\Data;
class LivePageBlock extends \Magento\Framework\View\Element\Template
{
    protected  $_config;
    protected $moduleReader;
    protected $_logo;
    protected $_productCollectionFactory;
    protected $_objectManager;
    protected $_imageHelper;
    protected $_unserialize;
    public function __construct(
        Unserialize $unserialize,
        \Magento\Theme\Block\Html\Header\Logo $logo,
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Module\Dir\Reader $moduleReader,
        CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Helper\Image $imageHelper,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        Data $config,
        array $data = []
    )
    {
        $this->moduleReader = $moduleReader;
        $this->_config=$config;
        $this->_logo=$logo;
        $this->_unserialize=$unserialize;
        $this->_productCollectionFactory=$productCollectionFactory;
        $this->_objectManager=$objectManager;
        $this->_imageHelper=$imageHelper;
        parent::__construct($context, $data);
    }
    public function getEnable(){
        return $this->_config->getGeneralConfig('enable');
    }
    public function getLogo(){
        return $this->_logo->getLogoSrc();
    }
    public function getLiveUrl(){
        return $this->_config->getGeneralConfig('live');
    }
    public function  getliveenable(){
        return $this->_config->getGeneralConfig('liveenable');
    }
    public function getVideoTitle1(){
        return $this->_config->getGeneralConfig('video_title1');
    }

    public function getVideo1(){
        return $this->_config->getGeneralConfig('video1');
    }
    public function getVideoView1(){
        return $this->_config->getGeneralConfig('video_view1');
    }
    public function getVideoTime1(){
        return $this->_config->getGeneralConfig('video_time1');
    }
    public function getVideoTitle2(){
        return $this->_config->getGeneralConfig('video_title2');
    }
    public function getVideo2(){
        return $this->_config->getGeneralConfig('video2');
    }
    public function getVideoView2(){
        return $this->_config->getGeneralConfig('video_view2');
    }
    public function getVideoTime2(){
        return $this->_config->getGeneralConfig('video_time2');
    }
    public function getSocket(){
        return $this->_config->getGeneralConfig('websocket');
    }
    public function getVideo(){
        if ($this->_config->getGeneralConfig('video')){
            return $this->_unserialize->unserialize($this->_config->getGeneralConfig('video'));
        }

    }
    public function mobile_backgourd(){

    }
    /**
     * 获取背景图
     * @return string
     */
    public function getBackgroud()
    {
        if ($this->_config->getGeneralConfig('background')) {
            return $this->getUrl('media/logo') . $this->_config->getGeneralConfig('background');
        }
    }
    /**
     * 获取背景图
     * @return string
     */
    public function getBrandStoryLogo()
    {
        if ($this->_config->getGeneralConfig('brand_story_logo')) {
            return $this->getUrl('media/logo') . $this->_config->getGeneralConfig('brand_story_logo');
        }
    }

    /**
     * 轮播文字
     * @return mixed
     */
    public function getLunBo()
    {
      if ($this->_config->getGeneralConfig('lunbo_text')){
         return   $this->_unserialize->unserialize( $this->_config->getGeneralConfig('lunbo_text'));
      }


    return [];


    }
    /**
     * 品牌简介
     */
    public function getBrandStoryDesc(){
        return $this->_config->getGeneralConfig('brand_story_desc');
    }
    public function getActivitesLogo(){
        if ($this->_config->getGeneralConfig('Activities')) {
            return $this->getUrl('media/logo') . $this->_config->getGeneralConfig('Activities');
        }
    }
    public function getPhone(){
        return $this->_config->getGeneralConfig('phone');
    }


    public function getEmail(){
        return $this->_config->getGeneralConfig('email');
    }

    public function getContact(){
        return $this->_config->getGeneralConfig('contact');
    }
    /**
     * 得到skuProduct
     */
    public function getSkuProduct(){
        $productsSkus= $this->_config->getGeneralConfig('all_products');
        $skus=explode(',',$productsSkus);
        $productCollection = $this->_productCollectionFactory->create();
        $productCollection->addFieldToFilter('sku', ['in' => $skus]);
        $products=[];
        foreach ($productCollection as $k => $item) {
            $_product = $this->_objectManager->create('Magento\Catalog\Model\Product')->load($item->getId());
            $products[$k]['id'] = $_product->getId();
            $products[$k]['price'] = $_product->getFinalPrice();
            $products[$k]['total_price']=$_product->getPrice();
            $products[$k]['name'] = $_product->getName();
            $products[$k]['url'] = $_product->getProductUrl();
            if (@$_product->getImage()) {
                $products[$k]['image'] = $this->_imageHelper->init($_product->getImage(), 'small_image')
                    ->setImageFile($_product->getImage()) // image,small_image,thumbnail
                    ->resize(380)
                    ->getUrl();
            } else {
                $products[$k]['image'] = '';
            }
        }
        return $products;
    }
}